# -*- coding: utf-8 -*-
from odoo import models, fields, api
from odoo.exceptions import ValidationError


class Equipment(models.Model):
    """设备主数据 - 基于maintenance.equipment简化版"""
    _name = 'production.equipment'
    _description = '设备主数据'
    _rec_name = 'name'
    _order = 'name'
    
    # 基本信息
    name = fields.Char(
        string='设备名称',
        required=True,
        help='设备的名称'
    )
    
    equipment_code = fields.Char(
        string='设备编号',
        required=True,
        help='设备的唯一编号'
    )
    
    company_id = fields.Many2one(
        'res.company',
        string='公司',
        default=lambda self: self.env.company,
        help='所属公司'
    )
    
    active = fields.Boolean(
        string='激活',
        default=True,
        help='是否激活此设备'
    )
    
    # 分类信息
    category_id = fields.Many2one(
        'production.equipment.category',
        string='设备类别',
        help='设备所属类别'
    )
    
    department_id = fields.Many2one(
        'hr.department',
        string='部门',
        help='负责此设备的部门'
    )
    
    workcenter_id = fields.Many2one(
        'production.workcenter',
        string='工作中心',
        help='设备所属的工作中心'
    )
    
    # 技术信息
    model = fields.Char(
        string='型号',
        help='设备型号'
    )
    
    serial_no = fields.Char(
        string='序列号',
        help='设备序列号'
    )
    
    manufacturer = fields.Char(
        string='制造商',
        help='设备制造商'
    )
    
    manufacture_date = fields.Date(
        string='制造日期',
        help='设备制造日期'
    )
    
    purchase_date = fields.Date(
        string='采购日期',
        help='设备采购日期'
    )
    
    warranty_date = fields.Date(
        string='保修期至',
        help='设备保修截止日期'
    )
    
    # 位置信息
    location = fields.Char(
        string='位置',
        help='设备所在位置'
    )
    
    # 技术参数
    capacity = fields.Float(
        string='产能',
        help='设备理论产能'
    )
    
    capacity_uom = fields.Char(
        string='产能单位',
        default='件/小时',
        help='产能计量单位'
    )
    
    power = fields.Float(
        string='功率(kW)',
        help='设备功率'
    )
    
    voltage = fields.Char(
        string='电压',
        help='工作电压'
    )
    
    # 状态信息
    status = fields.Selection([
        ('available', '可用'),
        ('in_use', '使用中'),
        ('maintenance', '维护中'),
        ('repair', '维修中'),
        ('standby', '待机'),
        ('retired', '已报废'),
    ], string='状态', default='available', help='设备当前状态')
    
    # 备注
    note = fields.Text(
        string='备注',
        help='其他说明信息'
    )
    
    # 唯一性约束
    _sql_constraints = [
        ('equipment_code_company_unique',
         'UNIQUE(equipment_code, company_id)',
         '设备编号在同一公司内必须唯一！')
    ]
    
    def name_get(self):
        """显示名称格式"""
        result = []
        for record in self:
            name = f"[{record.equipment_code}] {record.name}"
            result.append((record.id, name))
        return result


class EquipmentCategory(models.Model):
    """设备类别"""
    _name = 'production.equipment.category'
    _description = '设备类别'
    _order = 'name'
    
    name = fields.Char(
        string='类别名称',
        required=True,
        help='设备类别名称'
    )
    
    code = fields.Char(
        string='类别编码',
        help='类别编码'
    )
    
    description = fields.Text(
        string='描述',
        help='类别描述'
    )
    
    active = fields.Boolean(
        string='激活',
        default=True
    )
    
    equipment_ids = fields.One2many(
        'production.equipment',
        'category_id',
        string='设备列表'
    )
    
    equipment_count = fields.Integer(
        string='设备数量',
        compute='_compute_equipment_count'
    )
    
    @api.depends('equipment_ids')
    def _compute_equipment_count(self):
        for record in self:
            record.equipment_count = len(record.equipment_ids)
    
    _sql_constraints = [
        ('code_unique',
         'UNIQUE(code)',
         '类别编码必须唯一！')
    ]

